<template>
  <el-select
    :value="value"
    filterable
    clearable
    :multiple="multiple"
    :disabled="disabled"
    :placeholder="placeholder"
    style="width: 100%;"
    @input="change($event)"
  >
    <el-option
      v-for="(value, key) in selectOptionMap"
      :key="key"
      :label="value"
      :value="key"
    />
  </el-select>
</template>

<script>
  import { getConstantCodeMap } from '@/utils/common'
  export default {
    name: 'CommonSelect',
    props: {
      typeCode: {
        type: String
      },
      value: {
        type: String
      },
      disabled: {
        type: Boolean,
        default: false
      },
      multiple: {
        type: Boolean,
        default: false
      },
      placeholder: {
        type: String,
        default: '请选择…'
      }
    },
    data () {
      return {
        selectOptionMap: {},
        selectvalue: ''
      }
    },
    mounted () {
      if (this.typeCode === 'YES_NO') {
        this.selectOptionMap = {'1': '是', '0': '否'}
      } else if (this.typeCode === 'SEX') {
        this.selectOptionMap = {'1': '男', '0': '女'}
      } else if (this.typeCode === 'USER_TYPE') { // 用户类别
        this.selectOptionMap = {'0': '管理员', '1': '学校', '2': '学生', '3': '测试'}
      } else if (this.typeCode === 'PERMISSION_TYPE') { // 数据权限级别
        this.selectOptionMap = {'all': '全部', 'dept': '学校级', 'self': '个人级'}
      } else {
        getConstantCodeMap(this.typeCode).then(({data}) => {
          this.selectOptionMap = data.map
        })
      }
    },
    methods: {
      change: function (val) {
        this.$emit('input', val)
      }
    }
  }
</script>
